package com.opages.transaction.message.common.domain;

import java.io.Serializable;

/**
 * 调用结果
 * @author daizhong.liu
 **/
public class HttpResult<T> implements Serializable {

    private static final long serialVersionUID = 5862631446480162953L;
    private int code;
    private String msg;
    private T data;

    public HttpResult() {
    }

    public HttpResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public HttpResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 判断返回结果是否成功[成功码:200]
     *
     * @Author daizhong.liu
     * @Date 2021/9/17 15:52
     **/
    public boolean ifSuccess() {
        if (this.code == HttpCode.SUCCESS.getCode()) {
            return true;
        }
        return false;
    }

    public boolean ifTrue(HttpCode invokeCode) {
        if(this.code == invokeCode.getCode()) {
            return true;
        }
        return false;
    }

    /**
     * 默认成功
     */
    public static <T> HttpResult<T> success() {
        return new HttpResult<T>(HttpCode.SUCCESS.getCode(), HttpCode.SUCCESS.getMessage());
    }
    /**
     * 默认成功
     */
    public static <T> HttpResult<T> success(String message) {
        return new HttpResult<T>(HttpCode.SUCCESS.getCode(), message);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> HttpResult<T> success(T data) {
        return new HttpResult<T>(HttpCode.SUCCESS.getCode(), HttpCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> HttpResult<T> success(T data, String message) {
        return new HttpResult<T>(HttpCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> HttpResult<T> failed(HttpCode errorCode) {
        return new HttpResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static <T> HttpResult<T> failed(HttpCode errorCode, String message) {
        return new HttpResult<T>(errorCode.getCode(), message, null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> HttpResult<T> failed(String message) {
        return new HttpResult<T>(HttpCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> HttpResult<T> failed() {
        return failed(HttpCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> HttpResult<T> validateFailed() {
        return failed(HttpCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> HttpResult<T> validateFailed(String message) {
        return new HttpResult<T>(HttpCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> HttpResult<T> unauthorized(T data) {
        return new HttpResult<T>(HttpCode.UNAUTHORIZED.getCode(), HttpCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> HttpResult<T> forbidden(T data) {
        return new HttpResult<T>(HttpCode.FORBIDDEN.getCode(), HttpCode.FORBIDDEN.getMessage(), data);
    }

    public int getCode() {
        return code;
    }

    public HttpResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public HttpResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public HttpResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "Result{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }
}
